package org.jeecg.modules.demo.yxkcqcorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soOrderDetail.service.ISoOrderDetailService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.yxkcqcorder.entity.YxKcqcOrder;
import org.jeecg.modules.demo.yxkcqcorder.service.IYxKcqcOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: yx_kcqc_order
 * @Author: jeecg-boot
 * @Date:   2022-11-10
 * @Version: V1.0
 */
@Api(tags="yx_kcqc_order")
@RestController
@RequestMapping("/yxkcqcorder/yxKcqcOrder")
@Slf4j
public class YxKcqcOrderController extends JeecgController<YxKcqcOrder, IYxKcqcOrderService> {
	@Autowired
	private IYxKcqcOrderService yxKcqcOrderService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;
	@Autowired
	private ISoOrderDetailService soOrderDetailService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;


	/**
	 * 分页列表查询
	 *
	 * @param yxKcqcOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "库存期初余额-分页列表查询")
	@ApiOperation(value="库存期初余额-分页列表查询", notes="库存期初余额-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/yxkcqcorder/YxKcqcOrderList")
	public Result<?> queryPageList(YxKcqcOrder yxKcqcOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<YxKcqcOrder> queryWrapper = QueryGenerator.initQueryWrapper(yxKcqcOrder, req.getParameterMap());
			Page<YxKcqcOrder> page = new Page<YxKcqcOrder>(pageNo, pageSize);
			IPage<YxKcqcOrder> pageList = yxKcqcOrderService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   添加
	 *
	 * @param yxKcqcOrder
	 * @return
	 */
	@AutoLog(value = "库存期初余额-添加")
	@ApiOperation(value="库存期初余额-添加", notes="库存期初余额-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxKcqcOrder yxKcqcOrder,HttpServletRequest req) {
		try {
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = yxKcqcOrderService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			map.put("maxCode",str==null?null:str);
			String name =precode+ "QC_";
			String resultCode = generateOddUtil.createCode(map,name);

			yxKcqcOrder.setQcOrder(resultCode);
			yxKcqcOrder.setShenhe(0);
			yxKcqcOrder.setCreateTime( new Date());

			yxKcqcOrderService.save(yxKcqcOrder);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误!");
		}
	}

	 /**
	  *  审核
	  *
	  * @param yxKcqcOrder
	  * @return
	  */
	 @AutoLog(value = "库存期初余额-审核")
	 @ApiOperation(value="库存期初余额-审核", notes="库存期初余额-审核")
	 @PostMapping(value = "/checkQcOrder")
	 public Result<?> checkQcOrder(@RequestBody YxKcqcOrder yxKcqcOrder,HttpServletRequest req) {
		 try{
			 // 获取登录用户信息
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 if(!sysUser.getId().equals(yxKcqcOrder.getSaleman())){
				 return Result.error(500,"您不是当前单据的指定审核人！");
			 }

			 if(yxKcqcOrder.getShenhe()==1){
				 return Result.error(500,"单据已审核，无需重复审核！");
			 }
			 yxKcqcOrder.setShenhe(1);
			 yxKcqcOrder.setShTime(new Date());
			 String tenantId = req.getHeader("tenant-id");
			 String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			if(yxKcqcOrderService.checkQcOrder(yxKcqcOrder,precode)){
				return Result.OK("审核成功！");
			}
			 return Result.error(300,"审核失败！");
		 }catch (Exception e){
		 	e.printStackTrace();
		 	return Result.error(500,"系统错误！");
		 }
	 }
	/**
	 *  弃审
	 *
	 * @param yxKcqcOrder
	 * @return
	 */
	@AutoLog(value = "库存期初余额-弃审")
	@ApiOperation(value="库存期初余额-弃审", notes="库存期初余额-弃审")
	@PostMapping(value = "/unCheckQcOrder")
	public Result<?> unCheckQcOrder(@RequestBody YxKcqcOrder yxKcqcOrder) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxKcqcOrder.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxKcqcOrder.getShenhe()==0){
				return Result.error(300,"请先审核单据！");
			}
			List<SoOrderDetail> soOrderDetails =soOrderDetailService.getByWoNo(yxKcqcOrder.getWoNo());
			if(!soOrderDetails.isEmpty()){
				return Result.error(300,"该单据已被选用，无法弃审！");
			}

			if(yxKcqcOrderService.unCheckQcOrder(yxKcqcOrder)){
				return Result.OK("弃审成功！");
			}
			return Result.error(300,"弃审失败！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  编辑
	 *
	 * @param yxKcqcOrder
	 * @return
	 */
	@AutoLog(value = "库存期初余额-编辑")
	@ApiOperation(value="库存期初余额-编辑", notes="库存期初余额-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxKcqcOrder yxKcqcOrder) {
		try{
			yxKcqcOrderService.updateById(yxKcqcOrder);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存期初余额-通过id删除")
	@ApiOperation(value="库存期初余额-通过id删除", notes="库存期初余额-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			YxKcqcOrder yxKcqcOrder = yxKcqcOrderService.getById(id);
			if(yxKcqcOrder.getShenhe()==1){
				return Result.error(300,"该单据已审核，无法删除！");
			}
			yxKcqcOrderService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "库存期初余额-批量删除")
	@ApiOperation(value="库存期初余额-批量删除", notes="库存期初余额-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxKcqcOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "库存期初余额-通过id查询")
	@ApiOperation(value="库存期初余额-通过id查询", notes="库存期初余额-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxKcqcOrder yxKcqcOrder = yxKcqcOrderService.getById(id);
		if(yxKcqcOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxKcqcOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxKcqcOrder
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxKcqcOrder yxKcqcOrder) {
        return super.exportXls(request, yxKcqcOrder, YxKcqcOrder.class, "yx_kcqc_order");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxKcqcOrder.class);
    }

}
